import org.apache.xmlbeans.impl.xb.xsdschema.Public;

import java.util.concurrent.TimeUnit;

public class Interrupt {
    public static void println(Object msg) {
        System.out.println(msg);
    }

    /**
     * interrupt的使用
     */
    public static void interrupt() {
        Thread thread = new Thread() {
            @Override
            public void run() {
                while (true) {
                    println(Thread.currentThread().isInterrupted());
                    println("系统监测");
                    if (Thread.interrupted()) {
                        // 处理中断逻辑，例如退出循环
                        println(Thread.currentThread().isInterrupted());
                        break;
                    }
                }
            }
        };
        thread.start();
        thread.interrupt();
    }

    public static void sleepAndInterrupt() {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                println("线程已启动");
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    println(Thread.currentThread().isInterrupted());
                    println(e.getMessage());
                }
            }
        });
        thread.start();
        thread.interrupt();
    }

    public static void main(String[] args) {
        sleepAndInterrupt();
    }
}